/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.documents.service.cas;

import com.documents.common.core.PageBean;
import com.documents.common.core.service.BaseSrv;
import com.documents.common.util.DateUtil;
import com.documents.htmlobj.DocumentAndCase;
import com.documents.pojo.Case;
import com.documents.pojo.Country;
import com.documents.pojo.Document;
import com.documents.pojo.Product;
import com.documents.service.country.CountryService;
import com.documents.service.product.ProductService;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.apache.commons.collections.map.ListOrderedMap;

/**
 *
 * @author Administrator
 */
public class CaseService extends BaseSrv<Case, Integer> {

    private ProductService productService;
    private CountryService countryService;

    public CountryService getCountryService() {
        return countryService;
    }

    public void setCountryService(CountryService countryService) {
        this.countryService = countryService;
    }

    public ProductService getProductService() {
        return productService;
    }

    public void setProductService(ProductService productService) {
        this.productService = productService;
    }
    
    

    public PageBean getPageBean(int page, int pageSize, Case doccase, String startTime, String endTime) {
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(pageSize);
        pageBean.setCurrentPage(page);
        String condition = "";
        Object[] parameters = new Object[10];
        int paramIndex = 0;
        if (doccase != null) {

            if (doccase.getCountry() != null && doccase.getCountry() > 0) {
                if (condition.trim().length() == 0) {
                    condition += "country_code like ?";
                } else {
                    condition += "and country_code like ?";
                }
                    Country cc = countryService.get(doccase.getCountry());
                    parameters[paramIndex] = cc.getCode()+"%";
                    paramIndex++;
           
            }
//            if (doccase.getCountryName() != null && !doccase.getCountryName().equals("")) {
//                 if (condition.trim().length() == 0) {
//                    condition += "country_name=?";
//                } else {
//                    condition += "and country_name=?";
//                }
//                
//                parameters[paramIndex] = doccase.getCountryName();
//                paramIndex++;
//            }

            if (doccase.getName() != null && !doccase.getName().equals("")) {
//                if (condition.trim().length() == 0) {
//                    condition += "name like '%?%'";
//                } else {
//                    condition += "and name like '%?%'";
//                }
                if (condition.trim().length() == 0) {
                    condition += "name =?";
                } else {
                    condition += "and name =?";
                }

                parameters[paramIndex] = doccase.getName();
                paramIndex++;
            }
            if (doccase.getProduct() != null && doccase.getProduct() > 0) {
                if (condition.trim().length() == 0) {
                    condition += " product_code like ?";

                } else {
                    condition += " and product_code like ?";
                }
                Product pp=  productService.get(doccase.getProduct());
                    
                 parameters[paramIndex] =pp.getCode()+"%";
                 paramIndex++;
            }
//            if (doccase.getProductName() != null && !doccase.getProductName().equals("")) {
//                if (condition.trim().length() == 0) {
//                    condition += " product_name=?";
//
//                } else {
//                    condition += " and product_name=?";
//                }
//                parameters[paramIndex] = doccase.getProductName();
//                paramIndex++;
//            }
            if (startTime != null && !startTime.equals("")) {
                Date start = DateUtil.parseDate(startTime);
                if (condition.trim().length() == 0) {
                    condition += " time>=?";

                } else {
                    condition += " and time>=?";
                }
                parameters[paramIndex] = start;
                paramIndex++;
            }
            if (endTime != null && !endTime.equals("")) {
                Date end = DateUtil.parseDate(endTime);
                if (condition.trim().length() == 0) {
                    condition += " time<=?";

                } else {
                    condition += " and time<=?";
                }
                parameters[paramIndex] = end;

            }
            Long allRow = this.findTotalCount(condition, parameters);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Case> list;

            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page - 1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }

            List<DocumentAndCase> tglist = new ArrayList();
            for (Case dCase : list) {
                String sql = "select name from document where id in(select document_id from document_case where case_id=" + dCase.getId() + ")";
                List groupNames = this.jdbcTemplate.queryForList(sql);
                String names = "";
                for (int i = 0; i < groupNames.size(); i++) {
                    ListOrderedMap map = (ListOrderedMap) groupNames.get(i);
                    String name = (String) map.get("name");
                    if (i == 0) {
                        names = name;
                    } else {
                        names = names + "," + name;
                    }
                }
                DocumentAndCase docCase = new DocumentAndCase(dCase.getId(), dCase.getCode(), dCase.getName(), dCase.getCountry(), dCase.getCountryName(), dCase.getProduct(), dCase.getProductName(), dCase.getTime(), dCase.getFillSummy());
                docCase.setDocName(names);
                tglist.add(docCase);
            }

            pageBean.setList(tglist);

        } else {
            Long allRow = this.findTotalCount("", null);
            int totalPage = PageBean.countTotalPage(pageSize, allRow.intValue()); // 总页数
            pageBean.setAllRow(allRow.intValue());
            pageBean.setTotalPage(totalPage);
            List<Case> list;
            if (page == 1) {
                list = this.findWithPaging(condition, parameters, "code", page - 1, pageSize);
            } else {
                list = this.findWithPaging(condition, parameters, "code", (page - 1) * pageSize, pageSize);
            }
            List<DocumentAndCase> tglist = new ArrayList();
            for (Case dCase : list) {
                String sql = "select name from document where id in(select document_id from document_case where case_id=" + dCase.getId() + ")";
                List groupNames = this.jdbcTemplate.queryForList(sql);
                String names = "";
                for (int i = 0; i < groupNames.size(); i++) {
                    ListOrderedMap map = (ListOrderedMap) groupNames.get(i);
                    String name = (String) map.get("name");
                    if (i == 0) {
                        names = name;
                    } else {
                        names = names + "," + name;
                    }
                }
                DocumentAndCase docCase = new DocumentAndCase(dCase.getId(), dCase.getCode(), dCase.getName(), dCase.getCountry(), dCase.getCountryName(), dCase.getProduct(), dCase.getProductName(), dCase.getTime(), dCase.getFillSummy());
                docCase.setDocName(names);
                tglist.add(docCase);
            }

            pageBean.setList(tglist);
        }
        return pageBean;
    }

    public Case saveInstance(Case type, String idStr) {

        Set groupList = new HashSet();
        if (idStr != null) {
            String ids[] = idStr.split(",");
            for (int i = 0; i < ids.length; i++) {
                Document doc = new Document();
                doc.setId(new Integer(ids[i]));
                groupList.add(doc);
            }
        }
        type.setDocuments(groupList);

        this.save(type);

        return type;
    }

    public Case getInstance(int caseId) {
        Case ca = this.get(caseId);
        Set<Document> tglist = new HashSet();
        String sql = "select id from document where id in (select document_id from document_case where case_id=" + ca.getId() + ")";
        List groupNames = this.jdbcTemplate.queryForList(sql);
        String names = "";
        for (int i = 0; i < groupNames.size(); i++) {
            ListOrderedMap map = (ListOrderedMap) groupNames.get(i);
            Integer id = (Integer) map.get("id");

        }
        DocumentAndCase docCase = new DocumentAndCase(ca.getId(), ca.getCode(), ca.getName(), ca.getCountry(), ca.getCountryName(), ca.getProduct(), ca.getProductName(), ca.getTime(), ca.getFillSummy());
        docCase.setDocName(names);
        //   tglist.add(docCase);                      

        ca.setDocuments(tglist);
        return ca;
    }
}
